package com.rylan.controller;


import com.rylan.entity.Room;
import com.rylan.entity.RoomVO;
import com.rylan.entity.User;
import com.rylan.entity.UserVO;
import com.rylan.feign.UserFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @author rylan
 * @description
 */
@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserFeign userFeign;
    private UserVO userVO;
    @Autowired
    public UserController(UserVO userVO) {
        this.userVO = userVO;
    }

    @GetMapping("/findAll")
    @ResponseBody
    public UserVO findAll(@RequestParam("page") int page, @RequestParam("limit") int limit){
        int index = (page-1)*limit;
        List<User> data = userFeign.findAll(index,limit);
        int count = userFeign.findAll(0,1000).size();
        userVO.setCode(0);
        userVO.setMsg("");
        userVO.setCount(count);
        userVO.setData(data);
        return userVO;
    }
    @GetMapping("/deleteById/{id}")
    public String deleteById(@PathVariable("id") long id){
        userFeign.deleteById(id);
        return "redirect:/user/mahjong/user_manage";
    }

    @GetMapping("/mahjong/{location}")
    public String redirect(@PathVariable("location") String location){
        return location;
    }


    @PostMapping("/save")
    public String save(User user){
        user.setRegisterdate(new Date());
        userFeign.save(user);
        return "redirect:/user/mahjong/user_manage";
    }
    @PostMapping("update")
    public String update(User user){
        userFeign.update(user);
        return "redirect:/user/mahjong/user_manage";
    }
    @GetMapping("/findById/{id}")
    public String findById(Model model,@PathVariable("id") long id){
        model.addAttribute("user",userFeign.findById(id));
        return "user_update";
    }
   @GetMapping("/count")
    public  int count(){
        return userFeign.count();
   }

}
